package com.ajie.jdbc;

import com.ajie.jdbc.dao.UserDao;
import com.ajie.jdbc.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author wyj
 * @since 2022/1/20
 */
public class Test02 {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        /**
         * 这里能拿到对应的代理类是因为接口路径作为mapper的namespace
         * 方法名是sql的方法名；不然的话也是拿不到代理对象的；
         * 在扫描UserMapper.xml的时候已经完成类对相关方法的绑定注入；
         * sqlSession.getMapper(UserDao.class)只是根据类型拿到
         *  已经缓存的代理对象或者生成对应的代理对象；
         */
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = userDao.selectUserById("4");
        System.out.println(user);
    }
}
